package com.whd.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whd.demo.common.Result;
import com.whd.demo.entity.Meeting;
import com.whd.demo.mapper.MeetingMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/meeting")
public class MeetingController {

    @Resource
    MeetingMapper meetingMapper;

    /**
     * 新增会面信息
     * @param meeting
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody Meeting meeting){
        meetingMapper.insert(meeting);
        return Result.success();
    }

    /**
     * 分页查询会面信息(租客）
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/tenant")
    public Result<?> findTenantPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam Integer id){
        Page<Meeting> meetingPage = meetingMapper.findTenantPage(new Page<>(pageNum,pageSize),id);
        return Result.success(meetingPage);
    }

    /**
     * 分页查询会面信息(房主）
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/owner")
    public Result<?> findOwnerPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam Integer id){
        Page<Meeting> meetingPage = meetingMapper.findOwnerPage(new Page<>(pageNum,pageSize),id);
        return Result.success(meetingPage);
    }

    /**
     * 租客查询自己的会面记录数
     * @param id
     * @return
     */
    @GetMapping("/selectTenantCount/{id}")
    public Integer selectTenantCount(@PathVariable Integer id){
        //LambdaQueryWrapper<Meeting> wrapper = Wrappers.<Meeting>lambdaQuery().eq(Meeting::getTenantId,id);
        //Integer count = meetingMapper.selectCount(wrapper);
        Integer count = meetingMapper.selectTenantCount(id);
        return count;
    }

    /**
     * 房主查询自己的会面记录数
     * @param id
     * @return
     */
    @GetMapping("/selectOwnerCount/{id}")
    public Integer selectOwnerCount(@PathVariable Integer id){
        //LambdaQueryWrapper<Meeting> wrapper = Wrappers.<Meeting>lambdaQuery().eq(Meeting::getTenantId,id);
        //Integer count = meetingMapper.selectCount(wrapper);
        Integer count = meetingMapper.selectOwnerCount(id);
        return count;
    }

    /**
     * 删除会面信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        meetingMapper.deleteById(id);
        return Result.success();
    }
}
